from PyQt5.QtWidgets import *
import sys
from PyQt5.QtWidgets import QWidget
sys.path.append("../")
from database.database import DataBase


class Demo(QWidget):
    def __init__(self,title_list, table_name, db_path="../database/plave_data.db",parent=None):
        super(Demo, self).__init__(parent)
        self.table_name = table_name
        self.title_list = title_list
        self.db_path = db_path
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.table_name)
        self.resize(844, 600)
        vhayout = QHBoxLayout()
        table = QTableWidget()
        data = DataBase(self.db_path)
        table_data, row = data.select_all(self.table_name)
        vol = len(table_data[0])
        data.close()
        table.setRowCount(row)
        table.setColumnCount(vol)
        table.setHorizontalHeaderLabels(self.title_list)
        for i in range(row):
            for j in range(vol):
                item_data = QTableWidgetItem(str(table_data[i][j]))
                table.setItem(i, j, item_data)
        
        table.resizeColumnsToContents()
        table.resizeRowsToContents()
        table.setAlternatingRowColors(True)
        vhayout.addWidget(table)
        self.setLayout(vhayout)

if __name__=="__main__":
    import sys
    app = QApplication(sys.argv)
    demo = Demo(table_name="User",title_list=["用户ID","用户名","密码"])
    # demo = Demo(table_name="Record",title_list=["交易ID","管理员ID","金额","具体菜品及其数量","时间"])
    demo.show()
    sys.exit(app.exec_())

